package com.example.calender

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.CalendarView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    private lateinit var dateTextView: TextView
    private lateinit var calendarView: CalendarView
    private lateinit var recyclerView: RecyclerView

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        dateTextView = findViewById(R.id.dateTextView)
        calendarView = findViewById(R.id.calendarView)

        calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
            val date = "$dayOfMonth/${month + 1}/$year"
            updateDateText(date)
            showToast("选择日期: $date")
        }

        // 初始化 RecyclerView
        initializeRecyclerView()
    }

    @SuppressLint("SetTextI18n")
    private fun updateDateText(date: String) {
        dateTextView.text = "日期: $date"
    }

    private fun showToast(message: String) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
        // 保存笔记到SharedPreferences
        saveNotesToSharedPreferences(loadNotesFromRecyclerView())
    }

    private fun initializeRecyclerView() {
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        // 加载笔记列表从SharedPreferences
        val notes = loadNotesFromSharedPreferences()
        recyclerView.adapter = NoteAdapter(this, notes)
    }
    @Throws(IllegalStateException::class)
    private fun loadNotesFromRecyclerView(): List<Note> {
        val adapter = recyclerView.adapter!!
        if (adapter is NoteAdapter) {
            return adapter.notes
        } else {
            throw IllegalStateException("Expected adapter to be an instance of NoteAdapter but found ${adapter::class.java.simpleName}")
        }
    }



    private fun saveNotesToSharedPreferences(notes: List<Note>) {
        val editor = getSharedPreferences("notes", MODE_PRIVATE).edit()
        for (i in notes.indices) {
            editor.putString("note_${i}_title", notes[i].title)
            editor.putString("note_${i}_description", notes[i].description)
        }
        editor.apply()
    }

    private fun loadNotesFromSharedPreferences(): List<Note> {
        val notes = mutableListOf<Note>()
        val preferences = getSharedPreferences("notes", MODE_PRIVATE)
        for (i in 0 until preferences.all.size step 2) {
            val title = preferences.getString("note_${i}_title", "")
            val description = preferences.getString("note_${i}_description", "")
            notes.add(Note(id = i, title = title.toString(), description = description.toString()))
        }
        return notes
    }


}